package com.burstly.lib.network.request;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import com.burstly.lib.downloadtracker.TrackDownloadRequest;
import com.burstly.lib.network.beans.ResponseBean;
import com.burstly.lib.network.beans.cookie.ICookieRequest;
import com.burstly.lib.persistance.ResponseSaver;
import com.burstly.lib.service.IHostProvider;
import com.burstly.lib.ui.BurstlyView;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.PrioritizedAsyncTask;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class RequestManager
{
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String TAG = "RequestManager";
  private static final List<BurstlyView> VIEWS = new ArrayList(10);
  private static ConnectivityManager sConnectivityManager;
  private static BroadcastReceiver sNetworkStateReceiver;
  private static TelephonyManager sTelephonyManager;
  private boolean mCachedLoaded;
  private final ResponseSaver<ResponseBean> mResponseSaver;

  public RequestManager(Context paramContext, String paramString)
  {
    this.mResponseSaver = new ResponseSaver(paramContext.getApplicationContext(), paramString);
  }

  public static void addToNetworkStateWatcher(BurstlyView paramBurstlyView)
  {
    if (!VIEWS.contains(paramBurstlyView))
      VIEWS.add(paramBurstlyView);
  }

  private static <T> AbortableAsyncTask<T> execute(BurstlyRequestTask<T> paramBurstlyRequestTask, RequestDataObject<T> paramRequestDataObject)
  {
    paramBurstlyRequestTask.setCallback(paramRequestDataObject.mCallback);
    paramBurstlyRequestTask.execute(new Void[0]);
    return paramBurstlyRequestTask;
  }

  public static ConnectionInfo getConnectionInfo()
  {
    ConnectionInfo localConnectionInfo = new ConnectionInfo("", "unknown", 0);
    NetworkInfo localNetworkInfo;
    if (sConnectivityManager != null)
    {
      localNetworkInfo = sConnectivityManager.getActiveNetworkInfo();
      if (localNetworkInfo == null)
        return localConnectionInfo;
    }
    else
    {
      return localConnectionInfo;
    }
    int i = localNetworkInfo.getType();
    if (i == 1)
      localConnectionInfo = new ConnectionInfo(localNetworkInfo.getTypeName(), localNetworkInfo.getSubtypeName(), 2);
    while (true)
    {
      return localConnectionInfo;
      if (i == 0)
        localConnectionInfo = new ConnectionInfo(localNetworkInfo.getTypeName(), localNetworkInfo.getSubtypeName(), 1);
    }
  }

  private static void initCore(Context paramContext)
  {
    Context localContext = paramContext.getApplicationContext();
    if (sConnectivityManager == null)
      sConnectivityManager = (ConnectivityManager)localContext.getSystemService("connectivity");
    if (sTelephonyManager == null)
      sTelephonyManager = (TelephonyManager)localContext.getSystemService("phone");
  }

  public static void initRquestManager(Context paramContext)
  {
    try
    {
      initCore(paramContext);
      Context localContext = paramContext.getApplicationContext();
      if (sNetworkStateReceiver == null)
      {
        sNetworkStateReceiver = new BroadcastReceiver()
        {
          public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent)
          {
            if (RequestManager.isOnline())
            {
              RequestManager.LOG.logDebug("RequestManager", "Network connected. Resuming autorefresh if any...", new Object[0]);
              Iterator localIterator = RequestManager.VIEWS.iterator();
              while (localIterator.hasNext())
              {
                BurstlyView localBurstlyView = (BurstlyView)localIterator.next();
                if (localBurstlyView.getDefaultSessionLife() > 0)
                  localBurstlyView.startDelayedRequest();
              }
            }
            RequestManager.LOG.logDebug("RequestManager", "Network disconnected.", new Object[0]);
          }
        };
        IntentFilter localIntentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        localContext.registerReceiver(sNetworkStateReceiver, localIntentFilter);
      }
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public static boolean isOnline()
  {
    NetworkInfo localNetworkInfo;
    if (sConnectivityManager != null)
    {
      localNetworkInfo = sConnectivityManager.getActiveNetworkInfo();
      if (localNetworkInfo != null);
    }
    else
    {
      return false;
    }
    return localNetworkInfo.isConnectedOrConnecting();
  }

  public static <T> AbortableAsyncTask<T> makeAdRequest(RequestDataObject<T> paramRequestDataObject, boolean paramBoolean)
  {
    if (isOnline())
    {
      BurstlyRequestTask localBurstlyRequestTask = new BurstlyRequestTask(paramRequestDataObject.mObjectToPost, paramRequestDataObject.mUri, paramRequestDataObject.mTargetClass, paramRequestDataObject.mBurstlyViewId);
      localBurstlyRequestTask.setCanBeAborted(paramBoolean);
      return execute(localBurstlyRequestTask, paramRequestDataObject);
    }
    onNoConnection(paramRequestDataObject.mCallback);
    return null;
  }

  public static AbortableAsyncTask<String> makeDownloadTrackRequest(Context paramContext, String paramString1, String paramString2, IRequestCallback<String> paramIRequestCallback)
  {
    initCore(paramContext);
    if (isOnline())
    {
      TrackDownloadRequest localTrackDownloadRequest = new TrackDownloadRequest(paramString2, paramContext, paramString1);
      localTrackDownloadRequest.setCallback(paramIRequestCallback);
      localTrackDownloadRequest.setCanBeAborted(false);
      return (AbortableAsyncTask)localTrackDownloadRequest.execute(new Void[0]);
    }
    onNoConnection(paramIRequestCallback);
    return null;
  }

  public static <T> AbortableAsyncTask<T> makeIpListRequest(RequestDataObject<T> paramRequestDataObject)
  {
    if (isOnline())
    {
      DownloadServerListTask localDownloadServerListTask = new DownloadServerListTask(paramRequestDataObject.mObjectToPost, paramRequestDataObject.mUri, paramRequestDataObject.mTargetClass, paramRequestDataObject.mBurstlyViewId);
      if (paramRequestDataObject.mHostProvider != null)
        localDownloadServerListTask.setHostProvider(paramRequestDataObject.mHostProvider);
      return execute(localDownloadServerListTask, paramRequestDataObject);
    }
    onNoConnection(paramRequestDataObject.mCallback);
    return null;
  }

  public static AbortableAsyncTask<Void> makeTrackEventRequest(String paramString1, String paramString2)
  {
    if (paramString1 == null)
      return null;
    if (isOnline())
    {
      SingleHostRequest localSingleHostRequest = new SingleHostRequest(paramString1, paramString2);
      localSingleHostRequest.setCanBeAborted(false);
      return (AbortableAsyncTask)localSingleHostRequest.execute(new Void[0]);
    }
    onNoConnection(null);
    return null;
  }

  private static void onNoConnection(IRequestCallback<?> paramIRequestCallback)
  {
    LOG.logWarning("RequestManager", "No network connection available. Can not call for ads.", new Object[0]);
    if (paramIRequestCallback != null)
      paramIRequestCallback.onNoConnection();
  }

  public static void removeFromNetworkStateWatcher(BurstlyView paramBurstlyView)
  {
    VIEWS.remove(paramBurstlyView);
  }

  public static void shutdown(Context paramContext)
  {
    try
    {
      if (sNetworkStateReceiver != null)
      {
        LOG.logInfo("RequestManager", "Unregistering network reciever...", new Object[0]);
        paramContext.getApplicationContext().unregisterReceiver(sNetworkStateReceiver);
      }
      sNetworkStateReceiver = null;
      sConnectivityManager = null;
      return;
    }
    finally
    {
    }
  }

  public ResponseSaver<ResponseBean> getResponseSaver()
  {
    return this.mResponseSaver;
  }

  public IAbortableRequest makeAdRequest(RequestDataObject<ResponseBean> paramRequestDataObject)
  {
    if ((!this.mCachedLoaded) && (this.mResponseSaver.isAllowedToLoad()))
    {
      LoadResponseTask localLoadResponseTask = new LoadResponseTask(paramRequestDataObject, this.mResponseSaver, this);
      localLoadResponseTask.execute(new Void[0]);
      this.mCachedLoaded = true;
      return localLoadResponseTask;
    }
    return makeAdRequest(paramRequestDataObject, true);
  }

  public static class ConnectionInfo
  {
    public static final int MOBILE_TYPE = 1;
    public static final int UNKNOWN_TYPE = 0;
    public static final int WIFI_TYPE = 2;
    private final int mCode;
    private final String mSubtipeName;
    private final String mTypeName;

    public ConnectionInfo(String paramString1, String paramString2, int paramInt)
    {
      this.mTypeName = paramString1;
      this.mSubtipeName = paramString2;
      this.mCode = paramInt;
    }

    public int getCode()
    {
      return this.mCode;
    }

    public String getSubtipeName()
    {
      return this.mSubtipeName;
    }

    public String getTypeName()
    {
      return this.mTypeName;
    }

    public String toString()
    {
      Object[] arrayOfObject = new Object[3];
      arrayOfObject[0] = Integer.valueOf(this.mCode);
      arrayOfObject[1] = this.mSubtipeName;
      arrayOfObject[2] = this.mTypeName;
      return MessageFormat.format("ConnectionInfo [mCode={0}, mSubtipeName={1}, mTypeName={2}]", arrayOfObject);
    }
  }

  private static class LoadResponseTask extends PrioritizedAsyncTask<ResponseBean>
    implements IAbortableRequest
  {
    private volatile boolean mAborted;
    private final IRequestCallback<ResponseBean> mCallback;
    private IAbortableRequest mRequest;
    private final RequestManager mRequestManager;
    private final RequestManager.RequestDataObject<ResponseBean> mRequestObject;
    private final ResponseSaver<ResponseBean> mResponseSaver;

    LoadResponseTask(RequestManager.RequestDataObject<ResponseBean> paramRequestDataObject, ResponseSaver<ResponseBean> paramResponseSaver, RequestManager paramRequestManager)
    {
      this.mRequestObject = paramRequestDataObject;
      this.mResponseSaver = paramResponseSaver;
      this.mCallback = paramRequestDataObject.mCallback;
      this.mRequestManager = paramRequestManager;
    }

    public void abortRequest()
    {
      this.mAborted = true;
      if (this.mRequest != null)
        this.mRequest.abortRequest();
    }

    protected void onPostExecute(ResponseBean paramResponseBean)
    {
      if (!this.mAborted)
      {
        if (paramResponseBean != null)
          this.mCallback.onSuccessUi(paramResponseBean);
      }
      else
        return;
      this.mRequest = this.mRequestManager.makeAdRequest(this.mRequestObject);
    }

    protected ResponseBean performInBackground()
    {
      ResponseBean localResponseBean = (ResponseBean)this.mResponseSaver.loadLastResponse();
      if (!this.mAborted)
      {
        if (localResponseBean != null)
        {
          localResponseBean.setIsLoadedFromCache(true);
          this.mCallback.onSuccessInBackground(localResponseBean);
        }
      }
      else
        return localResponseBean;
      this.mCallback.onFailInBackground(null);
      return localResponseBean;
    }
  }

  public static class RequestDataObject<T>
  {
    final String mBurstlyViewId;
    IRequestCallback<T> mCallback;
    IHostProvider mHostProvider;
    final ICookieRequest mObjectToPost;
    final Class<T> mTargetClass;
    final String mUri;

    public RequestDataObject(String paramString1, ICookieRequest paramICookieRequest, String paramString2, IRequestCallback<T> paramIRequestCallback, Class<T> paramClass)
    {
      this.mUri = paramString1;
      this.mObjectToPost = paramICookieRequest;
      this.mBurstlyViewId = paramString2;
      this.mCallback = paramIRequestCallback;
      this.mTargetClass = paramClass;
    }

    public RequestDataObject(String paramString1, ICookieRequest paramICookieRequest, String paramString2, IRequestCallback<T> paramIRequestCallback, Class<T> paramClass, IHostProvider paramIHostProvider)
    {
      this(paramString1, paramICookieRequest, paramString2, paramIRequestCallback, paramClass);
      this.mHostProvider = paramIHostProvider;
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.network.request.RequestManager
 * JD-Core Version:    0.6.2
 */